package com.dzl.admin;

import com.alibaba.fastjson.JSONObject;
import com.dzl.base.controller.BaseController;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.dzl.util.RedisUtil;
import org.springframework.web.bind.annotation.*;

/**
 * 缓存控制类
 * @author ChenLong
 * @date 2021-07-12 11:06:01
 * @return
 **/
@RestController
@RequestMapping("/cache")
public class CacheController extends BaseController{

	/**
	 * 缓存设置值
	 * @author ChenLong
	 * @date 2021-07-12 11:06:27
	 * @param obj 参数对象
	 * @return com.dzl.util.JsonResult
	 **/
	@PostMapping("/setValueForExchangeGoods")
	public JsonResult<String> updateCacheValueForExchangeGoods(@RequestBody JSONObject obj){
		String key = obj.getString("key");
		String value = obj.getString("value");
		if(EmptyUtils.isEmpty(key)) {
			return JsonResult.paramErrWithMsg("参数错误");
		}
		if(EmptyUtils.isEmpty(value)) {
			return JsonResult.paramErrWithMsg("参数错误");
		}
		RedisUtil.set(key,value);
		return JsonResult.success();
	}

	/**
	 * 获取缓存值
	 * @author ChenLong
	 * @date 2021-07-12 11:06:54
	 * @param key 参数key
	 * @return com.dzl.util.JsonResult
	 **/
	@GetMapping("/getValueForExchangeGoods")
	public JsonResult<Object> getCacheValueForExchangeGoods(String key){
		if(EmptyUtils.isEmpty(key)) {
			return JsonResult.paramErrWithMsg("参数错误");
		}
		Object object = RedisUtil.get(key);
		return JsonResult.success(object);
	}
}
